/*
 * Copyright (C) 2005-2017 Christoph Rupp (chris@crupp.de).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * See the file COPYING for License information.
 */

/*
 * A factory to create TxnOperation and TxnNode instances.
 */

#ifndef UPS_TXN_FACTORY_H
#define UPS_TXN_FACTORY_H

#include "0root/root.h"

#include "ups/types.h"

// Always verify that a file of level N does not include headers > N!
#include "1mem/mem.h"
#include "4txn/txn.h"

#ifndef UPS_ROOT_H
#  error "root.h was not included"
#endif

namespace upscaledb {

struct TxnFactory {
  // Creates a new TxnOperation
  static TxnOperation *create_operation(LocalTxn *txn,
            TxnNode *node, uint32_t flags, uint32_t orig_flags,
            uint64_t lsn, ups_key_t *key, ups_record_t *record) {
    TxnOperation *op;
    op = Memory::allocate<TxnOperation>(sizeof(*op)
                                            + (record ? record->size : 0)
                                            + (key ? key->size : 0));
    op->initialize(txn, node, flags, orig_flags, lsn, key, record);
    return op;
  }

  // Destroys a TxnOperation
  static void destroy_operation(TxnOperation *op) {
    op->destroy();
  }
};

} // namespace upscaledb

#endif /* UPS_TXN_FACTORY_H */
